/***
 **列表上部的控制栏
 ***/
define(['static', '../../management/setting', 'page'], function(CONST, setting) {
    extend(Main, Combine);

    function Main() {
        var _this = this;
        var _super = arguments.callee.parent;
        this.init = function() {
            _this._DOM = $("[data-com='control']");
            self.isRenderPage = true; //是否重新渲染分页
            this.render();
            this.event();
        };

        this.render = function(result) {
            this.renderPage(result);
            // this.renderRefreshTime(result);
        }

        this.event = function() {
            //展开折叠
            this._DOM.off('click', '.table-fold').on('click', '.table-fold', function(e) {
                _this._DOM.find('.table-fold').removeClass('open');
                $(this).addClass('open');
                fireEvent($(this).get(0), "fold-table", {open:$(this).find('.fa').hasClass('fa-align-right')});
            });
            this._DOM.off('change', 'select[name=per-page]').on('change', 'select[name=per-page]', function(e) {
                _this._DOM.find("div.ndp-page-wrapper[name=plain-page]").empty();
                var params = setting.getParams();
                if (setting.valicateParams(params)) {
                    _this._DOM.trigger('control-click');
                }
            });
            this._DOM.off('PAGE_CHANGE', 'div.ndp-page-wrapper[name=plain-page]').on('PAGE_CHANGE', 'div.ndp-page-wrapper[name=plain-page]', function(e) {
                var params = setting.getParams();
                if (setting.valicateParams(params)) {
                    _this._DOM.trigger('control-click');
                }
                self.isRenderPage = false; //是否重新渲染分页
            });
        }

        this.renderRefreshTime = function(result) {
            var tpl = '';
            if (result && result.data && result.data.refreshDatetime) {
                tpl = '<h5>上次更新时间：' + result.data.refreshDatetime + ' </h5>';
            }
            _this._DOM.find('.refresh-time').html(tpl);
        }
        this.renderPage = function(result) {
            if (self.isRenderPage) {
                _this._DOM.find("div.ndp-page-wrapper[name=plain-page]").empty();
                if (result && result.data && result.data.summary && result.data.summary.adSetCount) {
                    _this._DOM.find("div.ndp-page-wrapper[name=plain-page]").page({
                        type: 1,
                        totalItems: result.data.summary.adSetCount, //总共有n条数据
                        perPage: result.data._params.pSize,
                        currentPage: result.data._params.cPage
                    });
                }
            }
            self.isRenderPage = true;
        }

        this.getParams = function() {
            var params = {
                "pSize": _this._DOM.find('select[name=per-page]').val(), // 分页大小（必须项，正整数）
                "cPage": _this._DOM.find("div.ndp-page-wrapper[name=plain-page] li.page-item.active").text() || 1 // 请求第几页（必须项，从1开始）
            }
            return params;
        }

        /***
         ** 响应事件变化
         **/
        this.update = function(msg) {
            switch (msg.type) {
                case "scroll":
                    //_this._DOM.toggleClass("table-fixed",(msg.data.top<=70)?true:false);
                    break;
            }
            _super.update.call(_this, msg);
        }

    }
    return new Main();
});
